home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 09 - 1993 / 09.03 Mar 93 / Getting Started / Dialogger.c next >
Encoding:
C/C++ Source or Header  |  1993-01-12  |  8.4 KB  |  457 lines  |  [TEXT/KAHL]

  1. #define kBaseResID        128
  2. #define kAboutALRTid        129
  3. #define kDialogResID        128
  4.  
  5. #define kVisible            true
  6. #define kMoveToFront        (WindowPtr)-1L
  7. #define kNoGoAway            false
  8. #define kSleep                60L
  9.  
  10. #define kFirstRadio        3
  11.  
  12. #define kOn                    1
  13. #define kOff                0
  14.  
  15. #define iAfghan            3
  16. #define iElephant            4
  17. #define iSquirrel            5
  18.  
  19. #define iShowPreview        7
  20. #define iUserItem            8
  21.  
  22. #define kLeftMargin        5
  23. #define kTopMargin        40
  24.  
  25. #define mApple                kBaseResID
  26. #define iAbout                1
  27.  
  28. #define mFile                kBaseResID+1
  29. #define iSettings            1
  30. #define iQuit                3
  31.  
  32.  
  33. /*************/
  34. /*  Globals  */
  35. /*************/
  36.  
  37. Boolean        gDone, gShowPreview = true;
  38. short            gCurrentPICT = kBaseResID;
  39.  
  40.  
  41. /***************/
  42. /*  Functions  */
  43. /***************/
  44.  
  45. void        ToolBoxInit( void );
  46. PicHandle    LoadPICT( short picID );
  47. void        CreateWindow( void );
  48. void        MenuBarInit( void );
  49. void        EventLoop( void );
  50. void        DoEvent( EventRecord *eventPtr );
  51. void        HandleMouseDown( EventRecord *eventPtr );
  52. void        HandleMenuChoice( long menuChoice );
  53. void        HandleAppleChoice( short item );
  54. void        HandleFileChoice( short item );
  55. void        DoUpdate( EventRecord *eventPtr );
  56. void        DoDialog( void );
  57. void        FlipControl( ControlHandle control );
  58. void        DrawPreview( DialogPtr dialog, short picID );
  59. void        SwitchPICT( void );
  60.  
  61. /* see tech note 304 */
  62. pascal OSErr SetDialogDefaultItem(DialogPtr theDialog, short newItem) 
  63.     = { 0x303C, 0x0304, 0xAA68 };        
  64. pascal OSErr SetDialogCancelItem(DialogPtr theDialog, short newItem)
  65.     = { 0x303C, 0x0305, 0xAA68 };
  66.     
  67.  
  68. /******************************** main *********/
  69.  
  70. void    main( void )
  71. {
  72.     ToolBoxInit();
  73.     MenuBarInit();
  74.     
  75.     CreateWindow();
  76.     
  77.     EventLoop();
  78. }
  79.  
  80.  
  81. /*********************************** ToolBoxInit */
  82.  
  83. void    ToolBoxInit( void )
  84. {
  85.     InitGraf( &thePort );
  86.     InitFonts();
  87.     InitWindows();
  88.     InitMenus();
  89.     TEInit();
  90.     InitDialogs( NULL );
  91.     InitCursor();
  92. }
  93.  
  94.  
  95. /******************************** LoadPICT *********/
  96.  
  97. PicHandle    LoadPICT( short picID )
  98. {
  99.     PicHandle    pic;
  100.     
  101.     pic = GetPicture( picID );
  102.     
  103.     if ( pic == NULL )
  104.     {
  105.         SysBeep( 10 );    /*  Couldn't load the PICT resource!!!  */
  106.         ExitToShell();
  107.     }
  108. }
  109.  
  110.  
  111. /******************************** CreateWindow *********/
  112.  
  113. void    CreateWindow( void )
  114. {
  115.     WindowPtr    window;
  116.     PicHandle    pic;
  117.     Rect        r;
  118.     
  119.     pic = LoadPICT( gCurrentPICT );
  120.     
  121.     r = (**pic).picFrame;
  122.     
  123.     OffsetRect( &r, kLeftMargin - r.left,
  124.                     kTopMargin - r.top );
  125.     
  126.     window = NewWindow( NULL, &r, "\pMy Pet Fred", kVisible,
  127.             noGrowDocProc, kMoveToFront, kNoGoAway, 0L );
  128.     
  129.     if ( window == NULL )
  130.     {
  131.         SysBeep( 10 );    /*  Couldn't load the WIND resource!!!  */
  132.         ExitToShell();
  133.     }
  134.     
  135.     ShowWindow( window );
  136.     SetPort( window );
  137. }
  138.  
  139.  
  140. /****************** MenuBarInit ***********************/
  141.  
  142. void    MenuBarInit( void )
  143. {
  144.     Handle            menuBar;
  145.     MenuHandle        menu;
  146.     
  147.     menuBar = GetNewMBar( kBaseResID );
  148.     SetMenuBar( menuBar );
  149.  
  150.     menu = GetMHandle( mApple );
  151.     AddResMenu( menu, 'DRVR' );
  152.     
  153.     DrawMenuBar();
  154. }
  155.  
  156.  
  157. /******************************** EventLoop *********/
  158.  
  159. void    EventLoop( void )
  160. {        
  161.     EventRecord        event;
  162.     
  163.     gDone = false;
  164.     while ( gDone == false )
  165.     {
  166.         if ( WaitNextEvent( everyEvent, &event, kSleep, NULL ) )
  167.             DoEvent( &event );
  168.     }
  169. }
  170.  
  171.  
  172. /************************************* DoEvent     */
  173.  
  174. void    DoEvent( EventRecord *eventPtr )
  175. {
  176.     char    theChar;
  177.     
  178.     switch ( eventPtr->what )
  179.     {
  180.         case mouseDown: 
  181.             HandleMouseDown( eventPtr );
  182.             break;
  183.         case keyDown:
  184.         case autoKey:
  185.             theChar = eventPtr->message & charCodeMask;
  186.             if ( (eventPtr->modifiers & cmdKey) != 0 ) 
  187.                 HandleMenuChoice( MenuKey( theChar ) );
  188.             break;
  189.         case updateEvt:
  190.             DoUpdate( eventPtr );
  191.             break;
  192.     }
  193. }
  194.  
  195.  
  196. /************************************* HandleMouseDown */
  197.  
  198. void    HandleMouseDown( EventRecord *eventPtr )
  199. {
  200.     WindowPtr        window;
  201.     short            thePart;
  202.     long            menuChoice;
  203.     GrafPtr            oldPort;
  204.     long            windSize;
  205.     Rect            growRect;
  206.     
  207.     thePart = FindWindow( eventPtr->where, &window );
  208.     
  209.     switch ( thePart )
  210.     {
  211.         case inMenuBar:
  212.             menuChoice = MenuSelect( eventPtr->where );
  213.             HandleMenuChoice( menuChoice );
  214.             break;
  215.         case inSysWindow : 
  216.             SystemClick( eventPtr, window );
  217.             break;
  218.         case inContent:
  219.             SelectWindow( window );
  220.             break;
  221.         case inDrag : 
  222.             DragWindow( window, eventPtr->where, &screenBits.bounds );
  223.             break;
  224.     }
  225. }
  226.  
  227.  
  228. /****************** HandleMenuChoice ***********************/
  229.  
  230. void    HandleMenuChoice( long menuChoice )
  231. {
  232.     short    menu;
  233.     short    item;
  234.     
  235.     if ( menuChoice != 0 )
  236.     {
  237.         menu = HiWord( menuChoice );
  238.         item = LoWord( menuChoice );
  239.         
  240.         switch ( menu )
  241.         {
  242.             case mApple:
  243.                 HandleAppleChoice( item );
  244.                 break;
  245.             case mFile:
  246.                 HandleFileChoice( item );
  247.                 break;
  248.         }
  249.         HiliteMenu( 0 );
  250.     }
  251. }
  252.  
  253.  
  254. /****************** HandleAppleChoice ***********************/
  255.  
  256. void    HandleAppleChoice( short item )
  257. {
  258.     MenuHandle    appleMenu;
  259.     Str255        accName;
  260.     short        accNumber;
  261.     
  262.     switch ( item )
  263.     {
  264.         case iAbout:
  265.             NoteAlert( kAboutALRTid, NULL );
  266.             break;
  267.         default:
  268.             appleMenu = GetMHandle( mApple );
  269.             GetItem( appleMenu, item, accName );
  270.             accNumber = OpenDeskAcc( accName );
  271.             break;
  272.     }
  273. }
  274.  
  275.  
  276. /****************** HandleFileChoice ***********************/
  277.  
  278. void    HandleFileChoice( short item )
  279. {
  280.     short    newPICTid;
  281.     
  282.     switch ( item )
  283.     {
  284.         case iSettings:
  285.             DoDialog();
  286.             break;
  287.         case iQuit:
  288.             gDone = true;
  289.             break;
  290.     }
  291. }
  292.  
  293.  
  294. /************************************* DoUpdate     */
  295.  
  296. void    DoUpdate( EventRecord *eventPtr )
  297. {
  298.     PicHandle    pic;
  299.     WindowPtr    window;
  300.     Rect        r;
  301.     
  302.     window = (WindowPtr)eventPtr->message;
  303.     
  304.     pic = LoadPICT( gCurrentPICT );
  305.     
  306.     SetPort( window );
  307.     
  308.     BeginUpdate( window );
  309.     
  310.     r = window->portRect;
  311.     DrawPicture( pic, &r );
  312.     
  313.     EndUpdate( window );
  314. }
  315.  
  316.  
  317. /************************************* DoDialog     */
  318.  
  319. void    DoDialog( void )
  320. {
  321.     DialogPtr            dialog;
  322.     Boolean                dialogDone = false;
  323.     short                itemHit, itemType;
  324.     Handle                itemHandle;
  325.     Rect                itemRect;
  326.     
  327.     short            curRadioButton;
  328.     PicHandle        pic;
  329.     
  330.     dialog = GetNewDialog( kDialogResID, NULL, kMoveToFront );
  331.  
  332.     ShowWindow( dialog );
  333.     SetPort( dialog );
  334.     
  335.     SetDialogDefaultItem( dialog, ok );
  336.     SetDialogCancelItem( dialog, cancel );
  337.     
  338.     curRadioButton = gCurrentPICT - kBaseResID + kFirstRadio;
  339.     GetDItem( dialog, curRadioButton, &itemType, &itemHandle, &itemRect );
  340.     SetCtlValue( (ControlHandle)itemHandle, kOn );
  341.     
  342.     if ( gShowPreview )
  343.     {
  344.         GetDItem( dialog, iShowPreview, &itemType, &itemHandle, &itemRect );
  345.         SetCtlValue( (ControlHandle)itemHandle, kOn );
  346.     }
  347.     
  348.     DrawPreview( dialog, curRadioButton + kBaseResID - kFirstRadio );
  349.  
  350.     while ( ! dialogDone )
  351.     {
  352.         ModalDialog( NULL, &itemHit );
  353.         
  354.         switch( itemHit )
  355.         {
  356.             case ok:
  357.             case cancel:
  358.                 dialogDone = true;
  359.                 break;
  360.             case iShowPreview:
  361.                 GetDItem( dialog, iShowPreview, &itemType,
  362.                             &itemHandle, &itemRect );
  363.                 FlipControl( (ControlHandle)itemHandle );
  364.                 
  365.                 DrawPreview( dialog, curRadioButton + kBaseResID - kFirstRadio );
  366.                 break;
  367.             case iAfghan:
  368.             case iElephant:
  369.             case iSquirrel:
  370.                 if ( curRadioButton != itemHit )
  371.                 {
  372.                     GetDItem( dialog, curRadioButton, &itemType,
  373.                             &itemHandle, &itemRect );
  374.                     FlipControl( (ControlHandle)itemHandle );
  375.                     
  376.                     GetDItem( dialog, itemHit, &itemType,
  377.                             &itemHandle, &itemRect );
  378.                     FlipControl( (ControlHandle)itemHandle );
  379.                     
  380.                     curRadioButton = itemHit;
  381.                     
  382.                     DrawPreview( dialog, curRadioButton + kBaseResID - kFirstRadio );
  383.                 }
  384.                 break;
  385.         }
  386.     }
  387.     
  388.     HideWindow( dialog );
  389.     
  390.     if ( itemHit == ok )
  391.     {
  392.         GetDItem( dialog, iShowPreview, &itemType,
  393.                     &itemHandle, &itemRect );
  394.         if ( GetCtlValue( (ControlHandle)itemHandle ) == kOn )
  395.             gShowPreview = true;
  396.         else
  397.             gShowPreview = false;
  398.     
  399.         if ( gCurrentPICT != curRadioButton +
  400.                 kBaseResID - kFirstRadio )
  401.         {
  402.             gCurrentPICT = curRadioButton +
  403.                     kBaseResID - kFirstRadio;
  404.             SwitchPICT();
  405.         }
  406.     }
  407.     
  408.     DisposDialog( dialog );
  409. }
  410.  
  411.  
  412. /************************************* FlipControl     */
  413.  
  414. void    FlipControl( ControlHandle control )
  415. {
  416.     SetCtlValue( control, ! GetCtlValue( control ) );
  417. }
  418.  
  419.  
  420. /************************************* DrawPreview     */
  421.  
  422. void    DrawPreview( DialogPtr dialog, short picID )
  423. {
  424.     PicHandle    pic;
  425.     short        itemHit, itemType;
  426.     Handle        itemHandle;
  427.     Rect        itemRect;
  428.     
  429.     GetDItem( dialog, iShowPreview, &itemType, &itemHandle, &itemRect );
  430.     if ( GetCtlValue( (ControlHandle)itemHandle ) == kOff )
  431.     {
  432.         GetDItem( dialog, iUserItem, &itemType, &itemHandle, &itemRect );
  433.         EraseRect( &itemRect );
  434.         return;
  435.     }
  436.     
  437.     pic = LoadPICT( picID );
  438.     
  439.     GetDItem( dialog, iUserItem, &itemType, &itemHandle, &itemRect );
  440.     FrameRect( &itemRect );
  441.     
  442.     InsetRect( &itemRect, 1, 1 );
  443.     DrawPicture( pic, &itemRect );
  444. }
  445.  
  446.  
  447. /************************************* SwitchPICT     */
  448.  
  449. void    SwitchPICT( void )
  450. {
  451.     WindowPtr    window;
  452.     
  453.     window = FrontWindow();
  454.     DisposeWindow( window );
  455.     
  456.     CreateWindow();
  457. }